import { getBudgetList, deleteBudgetInfo } from "@/views/wy-operate/api/repair.js";
import { mapGetters } from "vuex"
import budgetDialog from './components/dialog'

export default {
    components: {
        budgetDialog,
    },
    provide() {
			return {
				initList: this.queryData,
			};
    },
    mounted() {
        this.queryData();
    },
    data() {
        return {
            // 表格列表数据
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 20,
                keyword: ''
            },
            // 选中的数据
            selectDataArr: [],
            // 分页
            pager: {
                total: 0, // 总页数
								current: 1, // 当前页数
								size: 20 // 每页显示多少条
            },
            // 页面loading
            loading: false,
        }
    },
    computed: {
        ...mapGetters(['permissions'])
    },
    methods: {
        // 数据请求
				queryData (params = {}) {
					this.loading = true;
					let requestData = { ...this.params, ...params };
					getBudgetList(requestData)
						.then(res => {
							const { current, size, total, records } = res.data.data;
							this.tableData = records;
							this.params = requestData;
							this.pager = { total, current, size };
						})
						.finally(() => {
							this.loading = false;
						});
				},
        // 数据删除
        deleteData() {
            if (this.selectDataArr.length == 0) {
                this.$alert("请勾选要删除的数据！");
                return;
            }
            let ids = this.selectDataArr.map(sd => sd.obtId);
            this.$confirm("此操作将永久删除该数据, 是否继续?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    deleteBudgetInfo(ids)
                        .then((rep) => {
                            this.$message({
                                type: "success",
                                message: "删除成功!",
                            });
                            this.queryData();
                        })
                        .catch((e) => {
                            this.$message({
                                type: "success",
                                message: "删除失败!",
                            });
                        });
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        //表格按钮点击事件监听
        handleBtnClick(type, row = {}){
            switch (type) {
                case 'preview':
                case 'edit':
                    this.$refs.editDialog.show({
                        type,
                        data: JSON.parse(JSON.stringify(row))
                    })
                    break;
                case 'delete':
                    this.$confirm("此操作将永久删除该数据, 是否继续?", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning",
                    }).then(() => {
                        console.log(row.obtId)
                        deleteBudgetInfo([row.obtId]).then((rep) => {
                            this.$message.success("删除成功!")
                            this.queryData({current: 1});
                        }).catch((e) => {
                            this.$message.fail("删除失败!")
                        });
                    }).catch(() => {
                        this.$message({
                            type: "info",
                            message: "已取消删除",
                        });
                    });
                    break;
                default:
                    break;
            }
        },
        // 添加
        toAdd() {
            this.$refs.editDialog.show({
                type: 'add'
            })
        },
			// 菜单点击监听
				handleMenuClick (type) {
					switch (type) {
						case "add":
							this.toAdd();
							break;
						case "delete":
							this.deleteData();
							break;
						default:
							this.$message(type);
							break;
					}
				}
    },
};
